package com.example.cia;

import java.io.IOException;

import com.example.cia.service.addDeviceService;
import com.example.cia.service.addSightsService;
import com.example.cia.support.IonClick;
import com.example.cia.support.QRcodeGet;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class addDevice extends Activity implements IonClick{
	
	Bitmap QRcodePicture;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add);

		//添加景点的button
		Button add = (Button) findViewById(R.id.addSightsButton);
		//添加设备的button
		Button addDevice = (Button) findViewById(R.id.addDeviceButton);
		
		add.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				EditText address = (EditText) findViewById(R.id.address);
				String detail = address.getText().toString();
				//向数据库中添加景点，获取生成ID
				int id = 0;
				
				id = new addSightsService().addSights(detail);
				//调用二维码生成方法
				
				try {
					QRcodePicture = new QRcodeGet().GetidQRcode(Integer.toString(id));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	
		        //调用完毕 ，返回一个bitmap对象
				AlertDialog.Builder QRcodeDisplay = new AlertDialog.Builder(addDevice.this);
				QRcodeDisplay.setTitle("生成二维码");
				if(QRcodePicture == null){
					QRcodeDisplay.setMessage("生成失败！");
					QRcodeDisplay.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
				
						@Override
						public void onClick(DialogInterface dialog, int which) {
				
						}
					});
				}else{
					ImageView pc = new ImageView(addDevice.this);
					pc.setImageBitmap(QRcodePicture);
					QRcodeDisplay.setView(pc);
					QRcodeDisplay.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
				
						@Override
						public void onClick(DialogInterface dialog, int which) {
							QRcodePicture = null;				
						}
					});
				}
				QRcodeDisplay.show();	
			};
		});
		
		addDevice.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//添加设备的信息需求
				EditText addDeviceId = (EditText) findViewById(R.id.addDeviceId);
				EditText addDeviceDescription = (EditText) findViewById(R.id.addDeviceDescription);
				EditText addDeviceState = (EditText) findViewById(R.id.addDeviceState);
				
				String deviceId = addDeviceId.getText().toString();
				String deviceDescription = addDeviceDescription.getText().toString();
				String deviceState = addDeviceState.getText().toString();
				//设备信息获取完毕，尝试添加到数据库
				
				addDeviceService addDevice =  new addDeviceService();
				if(addDevice.addDevice(deviceId, deviceDescription, Integer.parseInt(deviceState))){
					try {
						QRcodePicture = new QRcodeGet().GetidQRcode(deviceId);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}else{
					QRcodePicture = null;
				}
				//调用完毕 ，返回一个bitmap对象
				AlertDialog.Builder QRcodeDisplay = new AlertDialog.Builder(addDevice.this);
				QRcodeDisplay.setTitle("生成二维码");
				if(QRcodePicture == null){
					QRcodeDisplay.setMessage("生成失败！");
					QRcodeDisplay.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
				
						@Override
						public void onClick(DialogInterface dialog, int which) {
				
						}
					});
				}else{
					ImageView pc = new ImageView(addDevice.this);
					pc.setImageBitmap(QRcodePicture);
					QRcodeDisplay.setView(pc);
					QRcodeDisplay.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
				
						@Override
						public void onClick(DialogInterface dialog, int which) {					
							QRcodePicture = null;
						}
					});
				}
				QRcodeDisplay.show();	
			}
			
		});
	}
	
	public void checkDeviceOnClick(View v){
		Intent intent = new Intent(this,checkDevice.class);
		startActivity(intent);
	}
	
	public void addDeviceOnClick(View v){

	}
	
	public void aboutAppOnClick(View v){
		Intent intent = new Intent(this,aboutApp.class);
		startActivity(intent);
	}

	public void mainPageOnClick(View v) {
		Intent intent = new Intent(this,MainActivity.class);
		startActivity(intent);

	}  
	

}
